package com.handy.plugin;

import com.handy.lib.InitApi;
import com.handy.lib.api.MessageApi;
import com.handy.lib.util.BaseUtil;
import com.handy.lib.util.SqlManagerUtil;
import com.handy.playertitle.api.PlayerTitleApi;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * 主类
 *
 * @author handy
 **/
public class RpgPlayerTitle extends JavaPlugin {
    private static RpgPlayerTitle INSTANCE;
    private final static String LISTENER_PACKAGE_NAME = "com.handy.plugin.listener";

    @Override
    public void onEnable() {
        INSTANCE = this;
        // 初始化
        InitApi.getInstance(this).initListener(LISTENER_PACKAGE_NAME)
                .addMetrics(-1);
        this.loadPlayerTitle();
        this.loadRpgAttribute();
        MessageApi.sendConsoleMessage("§a已成功载入服务器！");
    }

    @Override
    public void onDisable() {
        // 关闭数据源
        SqlManagerUtil.getInstance().close();
        MessageApi.sendConsoleMessage("§a已成功卸载！");
    }

    public static RpgPlayerTitle getInstance() {
        return INSTANCE;
    }

    /**
     * 加载 RPGAttribute
     */
    private void loadRpgAttribute() {
        if (Bukkit.getPluginManager().isPluginEnabled("RPGAttribute")) {
            MessageApi.sendConsoleMessage(BaseUtil.replaceRpgChatColor("&a成功加载 RPGAttribute!"));
            return;
        }
        MessageApi.sendConsoleMessage(BaseUtil.replaceRpgChatColor("&4未找到 RPGAttribute!"));
    }

    /**
     * 加载 PlayerTitle
     */
    private void loadPlayerTitle() {
        if (Bukkit.getPluginManager().isPluginEnabled("PlayerTitle")) {
            MessageApi.sendConsoleMessage(BaseUtil.replaceRpgChatColor("&a成功加载 PlayerTitle!"));
            // 新增类型
            PlayerTitleApi.getInstance().addBuffType("RPGAttribute", "RPG属性", this.getName());
            return;
        }
        MessageApi.sendConsoleMessage(BaseUtil.replaceRpgChatColor("&4未找到 PlayerTitle!"));
    }

}